﻿using System;
using System.Collections.Generic;

namespace TDD.SpreadSheet
{
    public class Operators
    {
        private static List<Operator> _validOperators;
        static Operators()
        {
            _validOperators = new List<Operator>();
            _validOperators.Add(new Operator('*'));
            _validOperators.Add(new Operator('/'));
            _validOperators.Add(new Operator('+'));
            _validOperators.Add(new Operator('-'));
        }

        private static Operator GetOperator(char value)
        {
            return _validOperators.Find(delegate(Operator ope) { return ope.Name == value; });
        }

        public static bool IsOperator(char value)
        {
            Operator operatorFound = GetOperator(value);
            return operatorFound != null;
        }

        public static int GetOperatorOrder(char value)
        {
            Operator operatorFound = GetOperator(value);
            if (operatorFound == null)
            {
                throw new ArgumentException("The operator provided is not a valid operator.");
            }
            return operatorFound.Order;
        }

        public static decimal Calculate(char currentOperator, decimal valueA, decimal valueB)
        {
            Operator operatorFound = GetOperator(currentOperator);
            if (operatorFound == null)
            {
                throw new ArgumentException("The operator provided is not a valid operator.");
            }
            return operatorFound.Calculate(valueA, valueB);
        }
    }
}
